package com.caidan.search.config;

import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.AllNestedConditions;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 允许上传文件类型配置类
 */

@Configuration
@Slf4j
//@Conditional(AllNestedConditions.class)
//@AllArgsConstructor
//@NoArgsConstructor
@ConfigurationProperties(prefix = "caidan")
public class AllowFileTypeProperties {
//    @Value("${caidan.allowFileType}")
    private List<String> allowFileType;
//    @Value("${caidan.maxFileSize}")
    private Integer maxFileSize;
    private static final String[] DEFAULT_FILE_ORIGIN_TYPE = new String[]{"mp3", "jpg", "jpeg", "png", "gif", "wav","mp4"};
    private static final Integer DEFAULT_FILE_ORIGIN_SIZE = 10;
    // 获取允许的文件类型
    public List<String> getAllowFileType() {
        if (this.allowFileType == null) {
            log.error("[AllowFileTypeProperties.getAllowFileType] Properties load failed, please check Nacos config.");
            this.allowFileType = new ArrayList<>(Arrays.stream(DEFAULT_FILE_ORIGIN_TYPE).collect(Collectors.toList()));
        }
        return this.allowFileType;
    }

    // 获取最大文件大小
    public Integer getMaxFileSize() {
        if (this.maxFileSize == null) {
            log.error("[AllowFileTypeProperties.getMaxFileSize] Properties load failed, please check Nacos config.");
            this.maxFileSize = DEFAULT_FILE_ORIGIN_SIZE;
        }
        return this.maxFileSize;
    }
}